/*
** team.c for  in /home/tessa_b//proj/zappy-2011/smap/trantor/team
** 
** Made by arnaud1 tessa
** Login   <tessa_b@epitech.net>
** 
** Started on  Thu Jul  7 08:24:39 2011 arnaud1 tessa
** Last update Thu Jul  7 09:06:31 2011 arnaud1 tessa
*/

#include	<stdlib.h>
#include	<stdio.h>
#include	<strings.h>
#include	"team.h"

void		team_init(t_team *t, int _max, char *_name)
{
  t_trantorian	**tr;
  int		size;

  size = (_max + 1) * sizeof(*tr);
  tr = malloc(size);
  if (tr == NULL)
    {
      fprintf(stderr, "team_init : out of memory");
      exit(1);
    }
  bzero(tr, size);
  t->members = tr;
  t->name = _name;
  t->nb_members = 0;
  t->max_members = _max;
}

int		team_add_member(t_team *t, t_trantorian *tr)
{
  if (t->nb_members < t->max_members)
    {
      t->members[t->nb_members] = tr;
      t->nb_members++;
      return (1);
    }
  printf("team_add_member : team is full !!!\n");
  return (0);
}

void		team_remove_member(t_team *t, t_trantorian *tr)
{
  int		ind;
  int		i;
  int		nb;

  i = 0;
  nb = t->nb_members;
  if (nb > 0)
    {
      while (i < nb)
	{
	  if (t->members[i] == tr)
	    {
	      t->members[i] = t->members[nb];
	      t->members[nb] = NULL;
	      t->nb_members--;
	      break;
	    }
	  i++;
	}
    } 
}

void		team_display(t_team * team)
{
    printf("nb_members : %d\n", team->nb_members);
    printf("max_members : %d\n", team->max_members);
    printf("name : %s\n", team->name);
}

/*
int		main(int ac, char **av)
{
  t_team	var;
  t_trantorian	tr;

  if (ac != 3)
    {
      printf("bad params\n");
      exit(1);
    }
  team_init(&var, atoi(av[1]), av[2]);
  team_display(&var);
  printf("---\n");
  team_add_member(&var, &tr);
  team_add_member(&var, &tr);
  team_display(&var);
  printf("---\n");
  team_remove_member(&var, &tr);
  team_display(&var);
  }*/
